package com.lx.algorithm.util;

import java.util.Arrays;

/**
 * Description:  常用工具类。 检查顺序 创建数组 比较交换
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-01 13:24:58
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-01     张李鑫                     1.0         1.0 Version
 */
public class SortUtils {

    /**
     * <p>
     *
     * </p>
     * @param  i 小的索引
     * @param  j 大的索引
     * @param  arr 交换数组
     * @return int[]
     * @Author zlx
     * @date 2021/9/1
     */
    public static int []  swap(int i,int j,int []arr){
        int temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
        return arr;
    }

    /**
     * <p>
     *    排序检查
     * </p>
     * @param  sortMethod
     * @param  maxSize
     * @return void
     * @Author zlx
     * @date 2021/9/1
     */
    public static void check(SortMethod sortMethod,int maxSize){
        int testTime=5000;
        int maxValue=100;

        for (int i = 0; i < testTime; i++) {
            int[] arr1=getArr(maxSize,maxValue);
            int []arr=Arrays.copyOf(arr1,arr1.length);
            sortMethod.sort(arr1);
            Arrays.sort(arr);
            if (!Arrays.equals(arr,arr1)){
                for (int j = 0; j < arr.length; j++) {
                    System.out.println(arr[j]+"::::"+arr1[j]);
                }
                break;
            }
        }

    }

    public static int [] getArr(int maxSize,int maxValue){
        int[]arr=new int[(int)((maxSize+1)*Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=(int)((maxValue+1)*Math.random())-(int)((maxValue*Math.random()));
        }
        return arr;
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }


    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        System.out.println("错误");
    }

}
